import json
from typing import Dict
from library_test.base_test import BaseTest


class AnsibleModuleMocker:
    def __init__(self, params: dict):
        self.params = params

    def fail_json(self, msg, changed=True, rc=1):
        pass


class BaseTestMindIEServiceConfig(BaseTest):

    @classmethod
    def get_module_path(cls):
        return "ascend_deployer.module_utils.deepseek_cntr.mindie_service_config"

    @classmethod
    def setUpClass(cls) -> None:
        super().setUpClass()
        from ascend_deployer.module_utils.deepseek_cntr.mindie_service_config import MindIEServiceConfig
        param = {}
        master_ip = "127.0.0.1"
        mindie_image_name = "mindie_image_name"
        model_weight_path = "test_path"
        cntr_id = "cntr_id"
        npu_info = {}
        worker_num = 1
        cls.mindie_service_cfg = MindIEServiceConfig(
            AnsibleModuleMocker(param),
            master_ip,
            mindie_image_name,
            model_weight_path,
            cntr_id,
            npu_info,
            worker_num,
        )

    @classmethod
    def parse_context_exception(cls, context) -> Dict:
        return json.loads(str(context.exception))

class TestMindIEServiceConfig(BaseTestMindIEServiceConfig):

    def test_modify_config(self):

        backend_config = "BackendConfig"
        model_deployer_config = "ModelDeployConfig"
        model_config = "ModelConfig"
        self.npu_info = {}
        self.mindie_service_cfg.data = ""
        self.mindie_service_cfg.modify_config()
        self.mindie_service_cfg.data = {}
        self.mindie_service_cfg.modify_config()
        self.mindie_service_cfg.data = {backend_config: {}}
        self.mindie_service_cfg.modify_config()
        self.mindie_service_cfg.data = {
            backend_config: {
                model_deployer_config: {}
            }
        }
        self.mindie_service_cfg.modify_config()
        self.mindie_service_cfg.data = {
            backend_config: {
                model_deployer_config: {
                    model_config: ""
                }
            }
        }
        self.mindie_service_cfg.modify_config()
        self.mindie_service_cfg.data = {
            backend_config: {
                model_deployer_config: {
                    model_config: [
                        dict()
                    ]
                }
            }
        }
        self.mindie_service_cfg.modify_config()
        self.assertEqual(
            self.mindie_service_cfg.data[backend_config][model_deployer_config][model_config][0]["modelWeightPath"],
            "test_path"
        )